<template>
  <div class="container">
    <div class="top">
      <el-input
        style="width: 300px"
        placeholder="请输入盘点单编码"
        v-model="search.code"
        clearable
      >
      </el-input>
      <el-date-picker
        value-format="yyyy-MM-dd"
        v-model="date"
        class="hidden-xs-only"
        type="daterange"
        :clearable="false"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      ></el-date-picker>
      <el-button type="primary" icon="el-icon-search" @click="handleSearch()"
        >查询</el-button
      >
      <el-button type="danger" icon="el-icon-delete" @click="empty()"
        >清空</el-button
      >
      <el-button type="info" icon="el-icon-tickets" @click="derive()"
        >差异导出
      </el-button>
    </div>
    <el-table
      max-height="600"
      :data="tableData"
      border
      style="width: 100%;margin-top: 5px;"
      @selection-change="handleSelectionChange"
      empty-text="暂无数据"
    >
      <el-table-column
        type="selection"
        width="50"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="serial_code"
        label="单据编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="add_admin"
        label="添加人"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="add_time"
        label="添加时间"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="typeMsg"
        label="类型"
        align="center"
      ></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            type="info"
            size="mini"
            icon="el-icon-info"
            @click="particulars(scope)"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      @page="handleSizeChange"
      @pagesize="handleCurrentChange"
      :pageData="pageData"
    ></pagination>
    <superCollectCheckDetail :dialog="detailDialog"></superCollectCheckDetail>
  </div>
</template>
<script>
import { getToken } from "@/utils/auth";
import Pagination from "@/components/pagination";
import SuperCollectCheckDetail from "@/components/check/superCollectCheckDetail";
import { diffList } from "@/api/check/superCheck";
export default {
  components: {
    pagination: Pagination,
    superCollectCheckDetail: SuperCollectCheckDetail
  },
  data() {
    return {
      //页面数据
      tableData: [],
      multipleSelection: [],
      search: {
        code: "",
        created_date: "",
        end_date: ""
      },

      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper"
      },
      detailDialog: {
        dialogVisible: false,
        id: ""
      },
      date: []
    };
  },
  created() {
    this.getData();
  },
  methods: {
    //获取页面数据
    getData() {
      let data = {
        limit: this.pageData.pageSize,
        page: this.pageData.page,
        serial_code: this.search.code,
        start_time: this.search.created_date,
        end_time: this.search.end_date
      };
      diffList(data)
        .then(res => {
          if (res.code == 0) {
            res.data.paginate.data.forEach(item => {
              switch (item.type) {
                case 0:
                  item.typeMsg = "全场";
                  break;
                case 1:
                  item.typeMsg = "分类";
                  break;
                case 2:
                  item.typeMsg = "商品";
                  break;
              }
            });
            this.tableData = res.data.paginate.data;
            this.pageData.page = res.data.paginate.current_page;
            this.pageData.pagesize = res.data.paginate.per_page;
            this.pageData.total = res.data.paginate.total;
          }
        })
        .catch(err => {
          console.log(err);
        });
    },

    //导出
    derive() {
      this.$confirm("是否导出?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          if (this.multipleSelection.length > 0) {
            let ids = [];
            this.multipleSelection.forEach(res => {
              ids.push(res.id);
            });
            let link = document.createElement("a");
            link.href =
              this.$axios.defaults.baseURL +
              "/smkapi/Stocktaking/exportDiffList?token=" +
              getToken() +
              "&ids=" +
              ids.join(",");
            link.click();
          } else {
            this.$message({
              type: "info",
              message: "请勾选内容"
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消导出"
          });
        });
    },
    //详情
    particulars(scope) {
      this.detailDialog.dialogVisible = true;
      this.detailDialog.id = scope.row.id;
    },
    // 搜索
    handleSearch() {
      if (this.date.length > 0) {
        this.search.created_date = this.date[0];
        this.search.end_date = this.date[1];
      }
      this.pageData.page = 1;
      this.pageData.total = null;
      this.getData();
    },
    //清空
    empty() {
      this.search = {
        code: "",
        created_date: "",
        end_date: ""
      };
      this.date = [];
    },

    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;
      this.getData();
    },

    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;
      this.getData();
    },
    //选取
    handleSelectionChange(val) {
      this.multipleSelection = val;
    }
  }
};
</script>
<style scoped>
.top {
  position: relative;
  /* display: flex; */
}
.pagination {
  text-align: right;
  padding: 10px;
}
</style>
